<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>发布评论</title>
    <style>
        li {
            width: 300px;
            height: 20px;
            margin: 5px 0px;
            background-color: pink;
        }

        a {
            float: right;
        }
    </style>
</head>

<body>
    <textarea name="" id="text" cols="30" rows="10"></textarea>
    <button>发布</button>
    <ul>

    </ul>
    <script>
        //获取按钮
        var btn = document.querySelector('button')
        //获取ul
        var ul = document.querySelector('ul')
        //h获取文本域
        var text = document.querySelector('#text')
        //注册事件
        btn.onclick = function () {
            if (text.value == '') {
                alert('请输入评论')
                return false
            } else {
                //创建元素
                var li = document.createElement('li')
                //现有li才能赋值
                li.innerHTML = text.value + '<a href="javascript:;">删除</a>';
                //添加元素
                ul.insertBefore(li, ul.children[0])
            }

        }
        var aa = document.querySelectorAll('a')
        //绑定循环事件
        for (var i = 0; i < aa.length; i++) {
            aa[i].onclick = function () {
                ul.removeChild(this.parentNode)
            }
        }
    </script>
</body>

</html>